# -*- coding: utf-8 -*-
"""
@Time : 2024/2/26 1:49 PM
@Author: imamhusan
@Des: 商品模型
"""
from tortoise import Model, fields


class Goods(Model):
    title_uy = fields.CharField(max_length=255, description='维文名称')
    title_ch = fields.CharField(max_length=255, description='中文名称')
    image = fields.CharField(max_length=512, description='图片连接')
    price = fields.IntField(description='商品价格')
    state = fields.IntField(description='状态 0：下架 1：上架', default=1)
    quantity = fields.IntField(description='库存量', default=1)
    views = fields.IntField(description='点击量', default=0)
    type = fields.ForeignKeyField(model_name='base.Type', related_name='type_goods', on_delete=fields.SET_NULL, null=True)
    create_time = fields.DatetimeField(auto_now_add=True, description='创建时间')
    update_time = fields.DatetimeField(auto_now=True, description='更新时间')

    def to_dict(self, lan: str = None) -> dict:

        if lan:
            title_dict = {'uy': self.title_uy, 'ch': self.title_ch}

            return {
                'id': self.pk,
                'title': title_dict[lan],
                'image': self.image,
                'price': self.price,
                'views': self.views,
                'quantity': self.quantity
            }
        else:
            return {
                'id': self.pk,
                'title_uy': self.title_uy,
                'title_ch': self.title_ch,
                'image': self.image,
                'price': self.price,
                'views': self.views,
                'quantity': self.quantity
            }

    class Meta:
        table = 'oz_goods'
        table_description = '商品表'


